<?xml version="1.0" encoding="UTF-8"?> <!-- Copyright Artur Figueira 2009 <arturfigueira@gmail.com> -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" encoding="UTF-8" method="xml"/>

<xsl:template match="Package" name="amethyst_Vistrails_Header">
	<xsl:param name="workFlowId" />
	<xsl:param name="workflowName" />
	
	<xsl:variable name="WORKFLOW_ID">
		<xsl:choose>
			<xsl:when test="$workFlowId != ''"><xsl:value-of select="$workFlowId" /></xsl:when>
			<xsl:otherwise><xsl:value-of select="@Id" /></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:variable name="WORKFLOW_NAME">
		<xsl:choose>
			<xsl:when test="$workflowName != ''"><xsl:value-of select="$workflowName" /></xsl:when>
			<xsl:otherwise><xsl:value-of select="@Name" /></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<workflow id="{$WORKFLOW_ID}" name="{$WORKFLOW_NAME}" version="1.0.0" vistrail_id="{$WORKFLOW_ID}_vistrails" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vistrails.org/workflow.xsd">
		<xsl:apply-templates match="WorkflowProcesses" />
	</workflow>
	
</xsl:template>

<xsl:template match="WorkflowProcesses" name="amethyst_Vistrails_Modules">
	<xsl:for-each select="WorkflowProcess">
		<xsl:text disable-output-escaping="yes">&lt;!--</xsl:text>WorkflowId: <xsl:value-of select="@Id" /> | WorkflowName: <xsl:value-of select="@Name" /> <xsl:text disable-output-escaping="yes">--&gt;</xsl:text>
		<xsl:call-template name="amethyst_Vistrails_Module" />
		<xsl:call-template name="amethyst_Vistrails_Connection" />
	</xsl:for-each>
</xsl:template>

<xsl:template name="amethyst_Vistrails_Module">
	<xsl:for-each select="Activities/Activity">

		<xsl:variable name="X_POS">
			<xsl:value-of select="NodeGraphicsInfos/NodeGraphicsInfo/Coordinates/@XCoordinate" />
		</xsl:variable>
		
		<xsl:variable name="Y_POS">
			<xsl:value-of select="NodeGraphicsInfos/NodeGraphicsInfo/Coordinates/@YCoordinate" />
		</xsl:variable>
		
		<module cache="1" id="{@Id}" name="{@Name}" version="1.3">
			<location id="{@Id}_location" x="{$X_POS}" y="{$Y_POS}" />
		</module>
		
	</xsl:for-each>
</xsl:template>

<xsl:template name="amethyst_Vistrails_Connection">
	<xsl:for-each select="Transitions/Transition">
		<xsl:variable name="TO_MODULE_ID"><xsl:value-of select="@To" /></xsl:variable>
		
		<xsl:variable name="FROM_MODULE_ID"><xsl:value-of select="@From" /></xsl:variable>
		
		<xsl:variable name="TO_MODULE_NAME">
			<xsl:value-of select="../../Activities/Activity[@Id=$TO_MODULE_ID]/@Name" />
		</xsl:variable>
		
		<xsl:variable name="FROM_MODULE_NAME" >
			<xsl:value-of select="../../Activities/Activity[@Id=$FROM_MODULE_ID]/@Name" />
		</xsl:variable>
		
		<connection id="{position()}">
    		<port id="{position()}1" moduleId="{$FROM_MODULE_ID}" moduleName="{$TO_MODULE_NAME}" type="source" />
    		<port id="{position()}2" moduleId="{$TO_MODULE_ID}" moduleName="{$FROM_MODULE_NAME}" type="destination" />
  		</connection>
		
	</xsl:for-each>
</xsl:template>


<!-- Nullify some Nodes -->
<xsl:template match="PackageHeader"></xsl:template>
<xsl:template match="Participants"></xsl:template>
<xsl:template match="Pools"></xsl:template>



</xsl:stylesheet> 